package org.spring.springboot.app.community.info.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.community.info.domain.vo.CommunityInfoInsertAndUpadteVO;
import org.spring.springboot.app.community.info.domain.vo.CommunityInfoSelectVO;
import org.spring.springboot.app.community.info.service.ICommunityInfoService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description="社区-小区管理操作接口",tags = ApiIndex.CommunityInfoController)
@RequestMapping(value = "/api/communityInfo")
@RestController
public class CommunityInfoController {

    @Autowired
    private ICommunityInfoService communityInfoService;

    @ApiOperation(value = "根据ID查询小区信息")
    @RequestMapping(value = "/find_by_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<CommunityInfoSelectVO> findById(
            @ApiParam(value = "小区id", required = true) @RequestParam String id
    ) {

        R<CommunityInfoSelectVO> result = new R<CommunityInfoSelectVO>();
        CommunityInfoSelectVO bo = communityInfoService.find(id);
        result.setData(bo);
        return result;
    }
    @ApiOperation(value = "根据机构ID查询下面的所有小区信息")
    @RequestMapping(value = "/find_by_office_id", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<CommunityInfoSelectVO>> findByOfficeId(
            @ApiParam(value = "机构id", required = true) @RequestParam String office_id
    ) {

        R<List<CommunityInfoSelectVO>> result = new R<List<CommunityInfoSelectVO>>();
        List<CommunityInfoSelectVO> list=communityInfoService.findByOfficeId(office_id);
        result.setData(list);
        return result;
    }
    @ApiOperation(value = "保存小区信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiParam(value = "小区对象", required = true) @RequestBody CommunityInfoInsertAndUpadteVO vo
    ) {
        R result = new R();
        int count=communityInfoService.save(vo);
        if(count<1){
            result.setCode(1);
            result.setMessage("保存失败");
        }
        return  result;
    }
    @ApiOperation(value = "修改小区信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(
            @ApiParam(value = "小区对象", required = true) @RequestBody CommunityInfoInsertAndUpadteVO vo
    ) {
        R result = new R();
        int count=communityInfoService.update(vo);
        if(count<1){
            result.setCode(1);
            result.setMessage("修改失败");
        }
        return  result;
    }
    @ApiOperation(value = "删除小区信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "小区id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        int count=communityInfoService.delete(id);
        if(count<1){
            result.setCode(1);
            result.setMessage("删除失败");
        }
        return  result;
    }
}
